<template>
  <div class="body">
    <el-form class="form" :model="form" ref="refForm" v-loading="loading.form">
      <el-form-item label="广告" :label-width="fromViewCofing.label_width">
        <el-input
          :value="form.advertising_name"
          :disabled="true"
          class="inp"
        ></el-input>
      </el-form-item>

      <el-form-item label="类型" :label-width="fromViewCofing.label_width">
        <el-input
          :value="form.transaction_type == 'recharge' ? '充值' : '退款'"
          :disabled="true"
          class="inp"
        ></el-input>
      </el-form-item>

      <el-form-item
        :rules="[{ required: true, message: '金额不能为空' }]"
        label="金额"
        :label-width="fromViewCofing.label_width"
      >
        <el-input v-model="form.money" class="inp" type="number"></el-input>
      </el-form-item>

      <el-form-item label="凭证图片" :label-width="fromViewCofing.label_width">
        <el-upload
          class="upload"
          :multiple="true"
          :action="uploading_img.api_uploading_img_url"
          :headers="uploading_img.headers"
          :limit="4"
          :show-file-list="false"
          :on-success="
            (res, file) => {
              handleUploadingImg('voucher_img', res, file);
            }
          "
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <span slot="tip" class="el-upload__tip tip"
            >只能上传jpg/png文件，且不超过2MB</span
          >
        </el-upload>

        <div style="display: flex; flex-wrap: wrap">
          <el-image
            v-for="(img, index) in form.voucher_img"
            :key="index"
            style="width: 100px; height: 100px; margin-right: 5px"
            :src="imgShow(img)"
            fit="cover"
          ></el-image>
        </div>
      </el-form-item>

      <el-form-item label="备注" :label-width="fromViewCofing.label_width">
        <el-input
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 16 }"
          v-model="form.remark"
          class="textarea"
        ></el-input>
      </el-form-item>

      <div class="form_footer">
        <el-button type="success" @click="fromSubmit('refForm')"
          >提交</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import { apiAdvertisingRecharge } from "@/api/apiFinance";
import {
  getUploadingImgUrl,
  getUploadingVideoUrl,
  getImgUrl,
} from "@/api/uploading";
import { arrayToString, stringToArray } from "@/utils/arrayUtil";
import { getToken } from "@/utils/auth";
export default {
  name: "Index",
  data() {
    return {
      init_form: {
        advertising_name: "",
        advertising_id: "",
        money: "",
        voucher_img: [],
        remark: "",
        transaction_type: "recharge",
      },
      form: {},
      uploading_img: {
        headers: {},
        api_uploading_img_url: "",
        api_uploading_video_url: "",
        dialogImageUrl: "",
        dialogImgVisible: false,
        disabled: false,
        fileList: [],
      },

      fromViewCofing: {
        input_width: "120px",
        label_width: "120px",
      },
      business_list: [],
      organization: {
        name: "",
      },
      loading: {
        dialog_create: false,
        table: false,
        form: false,
      },
    };
  },
  created() {
    this.form = this.init_form;
    var params = this.$route.query;
    if (params.id != 0) {
      this.form.advertising_id = params.id;
      this.form.transaction_type = params.transaction_type;
      this.form.advertising_name = params.advertising_name;
    }
    this.uploading_img.api_uploading_img_url = getUploadingImgUrl();
    this.uploading_img.api_uploading_video_url = getUploadingVideoUrl();
    this.uploading_img.headers = {
      token: getToken(),
    };
  },
  methods: {
    //图片展示
    imgShow(url) {
      return getImgUrl(url);
    },
    // 图片上传成功
    handleUploadingImg(field, res, file) {
      console.log([res, file, field]);
      if (res.code == 1) {
        var form = this.form;
        form[field].push(res.data.path);
        console.log(form, "formformformform");
        this.form = form;
        this.$forceUpdate();
      } else {
        this.$message.error(res.msg);
      }
    },
    // 图片删除
    handleRemoveLogo(file) {
      console.log("图片删除");
      var form = this.form;
      form["logo_img"] = "";
      this.form = form;
      // this.$refs.ref_uploadImg.$children[1].$el.style.display = ''
      this.$forceUpdate();
      console.log(file);
    },
    /**表单提交 */
    fromSubmit() {
      const _this = this;
      var regex = /^-?\d+(\.\d{1,2})?$/;
      if (!regex.test(_this.form.money)) {
        _this.$message.error("金额格式错误");
        return false;
      }

      this.$confirm("是否确认提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          _this.loading_start("form");
          var form = _this.form;
          var reqData = {};
          reqData.advertising_id = form.advertising_id;
          reqData.transaction_type = form.transaction_type;
          reqData.money = form.money;
          reqData.remark = form.remark;
          reqData.recharge_type = form.recharge_type;
          reqData.voucher_img = arrayToString(form.voucher_img);

          apiAdvertisingRecharge(reqData).then((response) => {
            _this.loading_end("form");
            _this.$message({
              message: response.msg,
              type: "success",
            });
            _this.$router.go(-1);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    // 加载开始...
    loading_start(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = true;setTimeout(() => {this.loading[key] = false;}, 5000);
        });
      } else {
        this.loading[keys] = true;setTimeout(() => {this.loading[keys] = false;}, 5000);
      }
    },
    // 加载结束...
    loading_end(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = false;
        });
      } else {
        this.loading[keys] = false;
      }
    },
  },
};
</script>

<style lang="scss">
.body {
  background-color: #fff !important;

  .title {
    font-size: 36px;
  }

  .form {
    width: 100%;
    height: 100%;

    .inp {
      width: 280px;
    }

    .textarea {
      width: 380px;
    }

    .upload {
      .tip {
        margin-left: 10px;
        color: #616161;
      }
    }

    .form_footer {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      background-color: #616161;
      padding: 10px;
      text-align: center;
    }
  }
}
</style>